import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/viewmodel/medecine/receive_medicine/receive_medine_list_view_model.dart';

import '../../../res/gaps.dart';
import '../../../widgets/common_widget.dart';

class ReceiveMedicineListPage extends BaseStatefulMvvmWidget {
  @override
  State<StatefulWidget> createState() => _ReceiveMedicineListState();
}

class _ReceiveMedicineListState extends BaseMvvmListState<
    ReceiveMedicineListViewModel, ReceiveMedicineListPage> {
  GlobalKey<ScaffoldState> key = GlobalKey();

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      key: key,
      backgroundColor: Colours.bg_color,
      appBar: AppBar(
        title: Text('收药入柜'),
        actions: [
          if (vm?.inBatchMode == true)
            Row(
              children: [
                TextButton(
                    onPressed: () {
                      if (viewModel.isAllChoose) {
                        setState(() {
                          viewModel.chooseItem.clear();
                        });
                      } else {
                        viewModel.allChoose();
                      }
                    },
                    child: CommonText((vm?.isAllChoose == true) ? '取消全选' : '全选',
                        textColor: Colors.blue)),
              ],
            ),
          if (vm?.inBatchMode != true)
            IconButton(onPressed: () {
              key.currentState?.openEndDrawer();
            }, icon: Icon(Icons.search))
        ],
      ),
      body: Column(
        children: [
          loadingContentWidget.setWeight(1),
          SizedBox(
            height: 12,
          ),
          if (vm?.inBatchMode != true)
            Row(
              children: [
                SizedBox(
                  width: 16,
                ),
                TextButton(
                  onPressed: () {
                    viewModel.changeToBatchMode();
                  },
                  child: CommonText('批量入柜', textColor: Colors.blue),
                  style: ButtonStyle(
                      shape: WidgetStateProperty.all(
                          StadiumBorder(side: BorderSide(color: Colors.blue)))),
                ).setWeight(1),
                SizedBox(
                  width: 20,
                ),
                TextButton(
                  onPressed: () {
                    viewModel.addMedicine();
                  },
                  child: CommonText('新  增', textColor: Colors.white),
                  style: ButtonStyle(
                      backgroundColor: WidgetStateProperty.all(Colors.blue),
                      shape: WidgetStateProperty.all(
                          StadiumBorder(side: BorderSide(color: Colors.blue)))),
                ).setWeight(1),
                SizedBox(
                  width: 16,
                ),
              ],
            ),
          if (vm?.inBatchMode == true)
            Row(
              children: [
                SizedBox(
                  width: 16,
                ),
                TextButton(
                  onPressed: () {
                    viewModel.cancelBatchModel();
                  },
                  child: CommonText('取消', textColor: Colors.grey),
                  style: ButtonStyle(
                      shape: WidgetStateProperty.all(
                          StadiumBorder(side: BorderSide(color: Colors.grey)))),
                ).setWeight(1),
                SizedBox(
                  width: 20,
                ),
                TextButton(
                  onPressed: () {
                    viewModel.batchClick();
                  },
                  child: CommonText('确认入柜', textColor: Colors.white),
                  style: ButtonStyle(
                      backgroundColor: WidgetStateProperty.all(Colors.blue),
                      shape: WidgetStateProperty.all(
                          StadiumBorder(side: BorderSide(color: Colors.blue)))),
                ).setWeight(2),
                SizedBox(
                  width: 16,
                ),
              ],
            ),
          SizedBox(
            height: 20,
          )
        ],
      ),
      endDrawer: endDraw(),
    );
  }

  Widget endDraw() {
    return Column(
      children: [
        AppBar(
          leading: Container(),
          actions: [Container()],
          title: Text('筛选'),
        ),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(
              height: 12,
            ),
            CommonText('长者姓名'),
            SizedBox(
              height: 8,
            ),
            NoBorderInput(
                hintText: '输入长者姓名',
                text: vm?.searchName ?? "",
                maxLines: 1,
                textInputAction: TextInputAction.search,
                onChanged: (str) {
                  viewModel.searchName = str;
                },
                onSubmitted: (str) {
                  viewModel.requestRefresh();
                })
                .addToContainer(
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)),

            SizedBox(
              height: 10,
            ),
            CommonText('房间号'),
            SizedBox(
              height: 8,
            ),
            NoBorderInput(
                hintText: '输入房间号',
                text: vm?.roomName ?? "",
                maxLines: 1,
                textInputAction: TextInputAction.search,
                onChanged: (str) {
                  viewModel.roomName = str;
                },
                onSubmitted: (str) {
                  viewModel.requestRefresh();
                })
                .addToContainer(
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)),


            SizedBox(height: 10,),

            CommonText('药品名称'),
            SizedBox(
              height: 8,
            ),
            NoBorderInput(
                hintText: '输入药品名称',
                text: vm?.medicineName ?? "",
                maxLines: 1,
                textInputAction: TextInputAction.search,
                onChanged: (str) {
                  viewModel.medicineName = str;
                },
                onSubmitted: (str) {
                  viewModel.requestRefresh();
                })
                .addToContainer(
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)),

            SizedBox(height: 10,),

            CommonText('收药类型'),
            SizedBox(
              height: 8,
            ),
            CommonText(vm?.selectedType?.pickName,hintText: '请选择')
                .addToContainer(
                width: double.infinity,
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)).onTap(() {
                  viewModel.chooseReceiveType();
            }),

            SizedBox(height: 10,),

            CommonText('入柜状态'),
            SizedBox(
              height: 8,
            ),
            CommonText(vm?.receiveStatus?.pickName??"",hintText: '请选择')
                .addToContainer(
                width: double.infinity,
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)).onTap(() {
                  viewModel.chooseStatus();
            }),
            SizedBox(height: 10,),
          ],
        ).addToContainer(
          padding: EdgeInsets.symmetric(horizontal: 16),
        ),
        SizedBox().setWeight(1),
        Gaps.dividerLine,
        SizedBox(height: 10,),
        Row(
          children: [
            SizedBox(width: 16,),
            TextButton(
              onPressed: () {
                key.currentState?.closeEndDrawer();
                viewModel.resetClick();
              },
              child: CommonText('重置',textColor: Colors.blue),
              style: ButtonStyle(
                  shape: WidgetStateProperty.all(RoundedRectangleBorder(
                      borderRadius: BorderRadius.horizontal(left: 20.radius),
                      side: BorderSide(color: Colors.blue)
                  ))
              ),)
                .setWeight(1),
            TextButton(
                onPressed: () {
                  key.currentState?.closeEndDrawer();
                  viewModel.requestRefresh();
                },
                child: CommonText('确定',textColor: Colors.white),
                style: ButtonStyle(
                    backgroundColor: WidgetStateProperty.all(Colors.blue),
                    shape: WidgetStateProperty.all(RoundedRectangleBorder(
                      borderRadius: BorderRadius.horizontal(right: 20.radius),
                      side: BorderSide(color: Colors.blue),
                    ))
                ))
                .setWeight(1),
            SizedBox(width: 16,),
          ],
        ),
        SizedBox(height: 12,)
      ],
    ).addToContainer(
        width: MediaQuery.of(context).size.width * 0.7,
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.horizontal(left: 12.radius)));
  }

  @override
  Widget createItemWidget(int index) {
    var item = viewModel.listItems[index];
    return Stack(
      alignment: Alignment.center,
      children: [
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                CommonText('${item.commonName??""}',
                    textColor: Colors.blue,
                    fontWeight: FontWeight.bold,
                    textSize: 18).setWeight(1),
                CommonText('${item.displayQuantity}',textColor: Colors.blue)
              ],
            ),
            CommonText('药品规格：${item.specifications}'),
            CommonText('用法：${item.usageType??""}'),
            CommonText('用量：${item.usageQuantity==null?'-':'${item.usageQuantity}${item.minUnitName}'}'),
            CommonText('用药频率：${item.usageFrequency??"-"}'),
            Row(
              children: [
                CommonText('入柜状态：'),
                CommonText('${item.statusName??""}', textColor: Colors.blue)
              ],
            ),
            CommonText('收药类型：${item.bizTypeName??""}'),
            CommonText('收药人：${item.executorName??"-"}'),
            CommonText('收药时间：${item.executorTime??"-"}'),
          ].insertWidget(SizedBox(
            height: 8,
          )),
        ),
        Column(
          children: [
            CommonText('${item.customerName}', fontWeight: FontWeight.bold, textSize: 17),
            SizedBox(
              height: 10,
            ),
            CommonText('${item.bedNo??""}'),
            SizedBox(
              height: 8,
            ),
            if (!viewModel.inBatchMode&&item.status==0)
              UnconstrainedBox(
                child: TextButton(
                  onPressed: () {
                    viewModel.singleReceive(item);
                  },
                  child: CommonText('入柜', textColor: Colors.white),
                  style: ButtonStyle(
                    backgroundColor: WidgetStateProperty.all(Colors.blue),
                    shape: WidgetStateProperty.all(StadiumBorder()),
                    visualDensity: VisualDensity.compact,
                    padding: WidgetStateProperty.all(EdgeInsets.zero),
                    minimumSize: WidgetStateProperty.all(Size(80, 38)),
                  ),
                ),
              )
          ],
        ).addToContainer(width: 80).setLocation(top: 30, right: 40),
        if (viewModel.inBatchMode&&item.status==0)
          Icon(
            viewModel.chooseItem.contains(item)
                ? Icons.check_circle
                : Icons.radio_button_unchecked_rounded,
            size: 30,
          ).setLocation(right: 0)
      ],
    ).onTap(() {
      if (viewModel.inBatchMode&&item.status==0) {
        viewModel.batchItemClick(item);
      }
    }).addToContainer(
        decoration:
            BoxDecoration(color: Colors.white, borderRadius: 6.borderRadius),
        padding: EdgeInsets.symmetric(horizontal: 10, vertical: 12));
  }

  @override
  ReceiveMedicineListViewModel createViewModel() {
    return ReceiveMedicineListViewModel(context);
  }

  @override
  EdgeInsets get listPadding =>
      EdgeInsets.symmetric(horizontal: 16, vertical: 12);

  @override
  Widget get separatorDivider => SizedBox(
        height: 12,
      );

  @override
  bool get canPullUp => true;
}
